package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;

import dbmanager.DBManager;

import utility.Splitstring;

public class Dictionary 
{
	DBManager database;
	String username;
	
	public Dictionary(DBManager database,String username) 
	{	
		this.database = database;
		this.username = username;	
	}
	
	public String parse(String message)
	{
		final char stringChar = '"';
		//String wordArray[];
		//String message;
		final String SYSTEMUSER="+911234567891";
		Splitstring sp = new Splitstring();
		//Splitting at spaces
		String []msgids = sp.splitString(message);

		String newAbrevation ;
		String msgid;
		String newMessage="";
		String msg="" ;
		
		int firstLength=0;
		int i=0;
		
		//Command cp = new Command();
		SpecialCharacter spCharacter = new SpecialCharacter();
		
		while(msgids.length!=firstLength)
		{
			//System.out.println("the value of first in run : "+first[firstLength]);
			msgid = msgids[firstLength];
			newAbrevation  = msgids[firstLength];
			firstLength++;
			if(msgid.length()==0)
			{
				i = spCharacter.getNextSpChar(0, message.toCharArray());
				newMessage = newMessage.concat(msgid+message.charAt(i));
				
				message = message.substring(i);
				continue;
			}
			try
			{
				i=message.indexOf(msgid);
				if(msgid.charAt(0) == stringChar)
				{	
					i = message.indexOf(stringChar,i+2);
					System.out.println("In stringChar");
					
					message = message.substring(i+1);
					if(message.length()>0)
					if(spCharacter.checkSpchar(message,0)==false)
						return "ERROR";
				}
				else
				{	
					i = spCharacter.getNextSpChar(i, message.toCharArray());
					message = message.substring(i);
					if(message.length()>0)
					if(spCharacter.checkSpchar(message,0)==false)
						return "ERROR";
				}
				
				while(true)
				{	
					try
					{
						ResultSet rs = database.getSelect("select msgid from dictionary where (userid = '"+username+"' or userid = '"+SYSTEMUSER+"')  and lower(abrevation) = '"+newAbrevation.toLowerCase()+"'");
						if(rs.next())
						{
							msg = newAbrevation;
							newAbrevation = rs.getString("msgid");
						}
						else
						{
							System.out.println("CommandParsing::parse()::MSGID : "+msgid+"\t message : "+message);
							if(message.length()>0)
								newMessage = newMessage.concat(msgid+message.charAt(0));
							else
								newMessage = newMessage.concat(msgid);
							System.out.println("CommandParsing::parse():: Message Obtained "+newMessage);
							break;
							//return "Error";
						}
						rs.close();
					}
					catch(Exception e)
					{
						//return(first);
						//System.out.println("abrevation1 thred "+first[firstLength]);
					}
				
			 		if(!newAbrevation.equals("system"))
			 		{
			 			continue;
			 		}
					else 
					{
						System.out.println("CommandParsing::parse()::MSGID : "+msgid+"\t message : "+message);
						if(message.length()>0)
							newMessage = newMessage.concat(msg+message.charAt(0));
						else
							newMessage = newMessage.concat(msgid);
						System.out.println("CommandParsing::parse():: Message Obtained "+newMessage);
						break;
					}
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
				//System.out.println("Main thredMessage "+newMessage);
			}
		}
		return(newMessage);
	}
	
	public String replaceDictionary(String msg)
	{
		try 
		{
			Hashtable<Object, Object>table = new Hashtable<Object, Object>();
			table.put("userid",username);
			//ResultSet rs = db.getSelect("Select * from dictionary where userid='"+userid+"'");
			msg = msg.replaceAll("\"","");
			//ResultSet rs = database.getSelect("Select msgid,abrevation,length(abrevation) x from dictionary where userid='"+username+"' order by x desc");
			ResultSet rs = database.getSelect("Select msgid,abrevation from dictionary where userid='"+username+"' ");
			while(rs.next())
			{
				String abrevation = rs.getString("abrevation");
				String replace= replaceAbrevation(table, abrevation);
				if(msg.contains(abrevation))
					msg = msg.replace(abrevation, replace);
			}	
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return msg;
	}
	
	
	public String replaceAbrevation(Hashtable< Object,Object>table,String Split)
	{
		table.put("abrevation", Split);
		ResultSet rs =database.getselect(table, "dictionary");
		try
		{
			if(rs.next())
			{
				Split = rs.getString("msgid");
				table.put("abrevation", Split);
				Split = replaceAbrevation(table,Split);
			}	
		}
		catch (SQLException e) 
		{
			System.out.println("SQLException  in checkInCommandabrevation::replecesFromDictionary::Reveresdictionary");
			e.printStackTrace();
		}	
	  return Split;
	}
	
	public Hashtable<Object,Object> replaceAbrevation(Hashtable< Object,Object>table)
	{
		Hashtable<Object,Object> dictionaryHash = new Hashtable<Object, Object>();
		
		String abrevation = table.get("abrevation").toString();
		
		dictionaryHash.put("msgid",abrevation);
		dictionaryHash.put("userid",username);
		
		ResultSet rs =database.getselect(dictionaryHash, "dictionary");
		try 
		{
			if(rs.next())
			{
				abrevation = rs.getString("abrevation");
				table.put("abrevation", abrevation);				
			}	
		} catch (Exception e) {
			// TODO: handle exception
		}		
		
		return table;
	}
	/*public static void main(String[] args) 
	{
		ExecutionTimer t = new ExecutionTimer();
		 t.start();
		DBManager db = new DBManager();
		db.getConnect();
		Dictionary dict = new Dictionary(db,"+919922918605");
		System.out.println(dict.parse("nm*God496|mobileno:12#605"));
		t.end();
		TimerRecordFile timerFile=new TimerRecordFile("control","Dictionary","main",t.duration());
	}*/

}
